home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / stdio / fvwrite.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  6KB  |  192 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Chris Torek.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)fvwrite.c    5.3 (Berkeley) 5/4/91";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #define KERNEL
  42. #include "ixemul.h"
  43.  
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include "local.h"
  47. #include "fvwrite.h"
  48.  
  49. /*
  50.  * Write some memory regions.  Return zero on success, EOF on error.
  51.  *
  52.  * This routine is large and unsightly, but most of the ugliness due
  53.  * to the three different kinds of output buffering is handled here.
  54.  */
  55. int __sfvwrite(FILE *fp, struct __suio *uio)
  56. {
  57.     register size_t len;
  58.     register char *p;
  59.     register struct __siov *iov;
  60.     register int w, s;
  61.     char *nl;
  62.     int nlknown, nldist;
  63.  
  64.     if ((len = uio->uio_resid) == 0)
  65.         return (0);
  66.     /* make sure we can write */
  67.     if (!fp || cantwrite(fp))
  68.         return (EOF);
  69.  
  70. #undef MIN    /* in <sys/param.h> */
  71. #define    MIN(a, b) ((a) < (b) ? (a) : (b))
  72. #define    COPY(n)      (void) bcopy((void *)p, (void *)fp->_p, (size_t)(n));
  73.  
  74.     iov = uio->uio_iov;
  75.     p = iov->iov_base;
  76.     len = iov->iov_len;
  77.     iov++;
  78. #define GETIOV(extra_work) \
  79.     while (len == 0) { \
  80.         extra_work; \
  81.         p = iov->iov_base; \
  82.         len = iov->iov_len; \
  83.         iov++; \
  84.     }
  85.     if (fp->_flags & __SNBF) {
  86.         /*
  87.          * Unbuffered: write up to BUFSIZ bytes at a time.
  88.          */
  89.         do {
  90.             GETIOV(;);
  91.             w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
  92.             if (w <= 0)
  93.                 goto err;
  94.             p += w;
  95.             len -= w;
  96.         } while ((uio->uio_resid -= w) != 0);
  97.     } else if ((fp->_flags & __SLBF) == 0) {
  98.         /*
  99.          * Fully buffered: fill partially full buffer, if any,
  100.          * and then flush.  If there is no partial buffer, write
  101.          * one _bf._size byte chunk directly (without copying).
  102.          *
  103.          * String output is a special case: write as many bytes
  104.          * as fit, but pretend we wrote everything.  This makes
  105.          * snprintf() return the number of bytes needed, rather
  106.          * than the number used, and avoids its write function
  107.          * (so that the write function can be invalid).
  108.          */
  109.         do {
  110.             GETIOV(;);
  111.             w = fp->_w;
  112.             if (fp->_flags & __SSTR) {
  113.                 if (len < w)
  114.                     w = len;
  115.                 COPY(w);    /* copy MIN(fp->_w,len), */
  116.                 fp->_w -= w;
  117.                 fp->_p += w;
  118.                 w = len;    /* but pretend copied all */
  119.             } else if (fp->_p > fp->_bf._base && len > w) {
  120.                 /* fill and flush */
  121.                 COPY(w);
  122.                 /* fp->_w -= w; */ /* unneeded */
  123.                 fp->_p += w;
  124.                 if (fflush(fp))
  125.                     goto err;
  126.             } else if (len >= (w = fp->_bf._size)) {
  127.                 /* write directly */
  128.                 w = (*fp->_write)(fp->_cookie, p, w);
  129.                 if (w <= 0)
  130.                     goto err;
  131.             } else {
  132.                 /* fill and done */
  133.                 w = len;
  134.                 COPY(w);
  135.                 fp->_w -= w;
  136.                 fp->_p += w;
  137.             }
  138.             p += w;
  139.             len -= w;
  140.         } while ((uio->uio_resid -= w) != 0);
  141.     } else {
  142.         /*
  143.          * Line buffered: like fully buffered, but we
  144.          * must check for newlines.  Compute the distance
  145.          * to the first newline (including the newline),
  146.          * or `infinity' if there is none, then pretend
  147.          * that the amount to write is MIN(len,nldist).
  148.          */
  149.         nlknown = 0;
  150.         nldist = 0;    /* XXX just to keep gcc happy */
  151.         do {
  152.             GETIOV(nlknown = 0);
  153.             if (!nlknown) {
  154.                 nl = memchr((void *)p, '\n', len);
  155.                 nldist = nl ? nl + 1 - p : len + 1;
  156.                 nlknown = 1;
  157.             }
  158.             s = MIN(len, nldist);
  159.             w = fp->_w + fp->_bf._size;
  160.             if (fp->_p > fp->_bf._base && s > w) {
  161.                 COPY(w);
  162.                 /* fp->_w -= w; */
  163.                 fp->_p += w;
  164.                 if (fflush(fp))
  165.                     goto err;
  166.             } else if (s >= (w = fp->_bf._size)) {
  167.                 w = (*fp->_write)(fp->_cookie, p, w);
  168.                 if (w <= 0)
  169.                      goto err;
  170.             } else {
  171.                 w = s;
  172.                 COPY(w);
  173.                 fp->_w -= w;
  174.                 fp->_p += w;
  175.             }
  176.             if ((nldist -= w) == 0) {
  177.                 /* copied the newline: flush and forget */
  178.                 if (fflush(fp))
  179.                     goto err;
  180.                 nlknown = 0;
  181.             }
  182.             p += w;
  183.             len -= w;
  184.         } while ((uio->uio_resid -= w) != 0);
  185.     }
  186.     return (0);
  187.  
  188. err:
  189.     fp->_flags |= __SERR;
  190.     return (EOF);
  191. }
  192.